class Promise {
    static PENDING = 'pending'
    static FULFILLED = 'fulfilled'
    static REJECTED = 'rejected'

    constructor(executor) {
        this.value = ''
        this.status = Promise.PENDING
        // 用来存在then中的回调函数
        this.callbacks = []
        try {
            executor(this.reject.bind(this), this.resolve.bind(this))
        } catch (e) {
            console.log(e)
        }
    }

    // 处理resolve
    resolve(value) {
        if (this.status === Promise.PENDING) {
            this.value = value
            this.status = Promise.FULFILLED
            setTimeout(() => {
                this.callbacks.map(callback => {
                    callback.onFulfilled(value)
                })
            })
        }
    }

    // reject
    reject(reason) {
        if (this.status === Promise.PENDING) {
            this.value = reason
            this.status = Promise.REJECTED
            setTimeout(() => {
                this.callbacks.map(callback => {
                    callback.onRejected(reason)
                })
            })
        }
    }

    // then 方法
    then(onFulfilled, onRejected) {
        if (typeof onFulfilled !== 'function') {
            onFulfilled = () => {
            }
        }
        if (typeof onRejected !== 'function') {
            onRejected = () => {
            }
        }

        return new Promise((resolve, reject) => {
            if (this.status === Promise.FULFILLED) {
                setTimeout(() => {
                    try {
                        onFulfilled(this.value)
                    } catch (e) {
                        onRejected(e)

                    }
                })
            }
            if (this.status === Promise.REJECTED) {
                setTimeout(() => {
                    try {
                        onRejected(this.value)
                    } catch (e) {
                        onRejected(e)
                    }
                })

            }

            if (this.status === Promise.PENDING) {
                this.callbacks.push({
                    onFulfilled: (value) => {
                        try {
                            onFulfilled(value)
                        } catch (e) {
                            onRejected(e)
                        }
                    },
                    onRejected: (reason) => {
                        try {
                            onFulfilled(reason)
                        } catch (e) {
                            onRejected(e)
                        }
                    }
                })
            }
        })
    }

    // 静态属性
    static  race(promises) {
        return new Promise((resolve, reject) => {
            if (!Array.isArray(promises)) {
                return reject(new Error('传入的参数必须得是数组格式'))
            }
            promises.map(promise => {
                promise.then(data => {
                    resolve(data)
                }, err => {
                    reject(err)
                })
            })
        })
    }

    // 静态属性， 通过类型就可以访问的
    static all(promises) {
        return new Promise((resolve, reject) => {
            let res = []
            let count = 0
            const len = promises.length
            if (!Array.isArray(promises)) {
                reject(new Error('传入的参数必须得是数组格式'))
            }
            promises.map((promise, index) => {
                promise
                    .then(data => {
                        // 这里注意返回的顺序应该和传入的顺序是一样的
                        res[index] = data
                        count++
                    })
                    .catch(e => {
                        reject(e)
                    })
                if (count === len) {
                    resolve(res)
                }
            })
        })
    }
}
